﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace WpfToolkit
{
    internal sealed class TagPannelTemplateSelector : DataTemplateSelector
    {
        private string _displayMemberPath;
        private DataTemplate _checkTemplate;
        public TagPannelTemplateSelector(string displayMemberPath)
        {
            _displayMemberPath = displayMemberPath;
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (_checkTemplate == null)
            {
                _checkTemplate = new DataTemplate();
                FrameworkElementFactory cb = new FrameworkElementFactory(typeof(CheckBox));
                cb.SetValue(CheckBox.ClickModeProperty, ClickMode.Press);
                cb.SetValue(CheckBox.PaddingProperty, new Thickness(15, 0, 15, 0));
                cb.SetValue(CheckBox.MarginProperty, new Thickness(5));
                cb.SetValue(CheckBox.HeightProperty, (double)24);

                var tagBinding = new Binding
                {
                    Path = new PropertyPath(".")
                };
                cb.SetBinding(CheckBox.TagProperty, tagBinding);

                var binding = new Binding
                {
                    Path = new PropertyPath(_displayMemberPath)
                };
                cb.SetBinding(CheckBox.ContentProperty, binding);

                _checkTemplate.VisualTree = cb;
                _checkTemplate.Seal();
            }

            return _checkTemplate;
        }
    }
}
